﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace olierFrm.Extensions.Mail
{
    public class MailHelper
    {
        /// <summary>
        /// SMTP服务发送邮件
        /// </summary>
        /// <param name="mailSubjct">邮件主题</param>
        /// <param name="mailBody">邮件内容(html)</param>
        /// <param name="mailFrom">发件地址</param>
        /// <param name="mailAddress">收件地址</param>
        /// <param name="HostIP">SMTP主机IP</param>
        /// <param name="Port">SMTP主机端口，一般是25</param>
        /// <returns></returns>
        public static string sendMail(string mailSubjct, string mailBody, string mailFrom, List<string> mailAddress, string HostIP, int Port) //发送邮件
        {
            string error = "";
            try
            {
                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();

                mm.IsBodyHtml = false;
                mm.Subject = mailSubjct;
                mm.Body = mailBody;
                mm.IsBodyHtml = true;

                mm.From = new System.Net.Mail.MailAddress(mailFrom);
                for (int i = 0; i < mailAddress.Count; i++)
                {

                    mm.To.Add(mailAddress[i]);
                }
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                sc.UseDefaultCredentials = false;

                sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                sc.Host = HostIP;
                sc.Port = Port;
                sc.Send(mm);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }




        public static string sendMail(string mailSubjct, string mailBody, string mailFrom, List<string> mailAddress, string HostIP, string filename, string username, string password, bool ssl) //发送邮件
        {
            string error = "";
            try
            {
                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();

                mm.IsBodyHtml = false;
                mm.Subject = mailSubjct;
                mm.Body = mailBody;
                mm.IsBodyHtml = true;
                mm.From = new System.Net.Mail.MailAddress(mailFrom);
                for (int i = 0; i < mailAddress.Count; i++)
                {

                    mm.To.Add(mailAddress[i]);
                }
                if (System.IO.File.Exists(filename))
                    mm.Attachments.Add(new System.Net.Mail.Attachment(filename));
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                sc.EnableSsl = ssl;
                sc.UseDefaultCredentials = false;
                System.Net.NetworkCredential nc = new System.Net.NetworkCredential(username, password);
                sc.Credentials = nc;
                sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                sc.Host = HostIP;
                sc.Port = 25;
                sc.Send(mm);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

        /// <summary>
        /// SMTP发送邮件
        /// </summary>
        /// <param name="mailSubjct">主题</param>
        /// <param name="mailBody">内容</param>
        /// <param name="mailFrom">发件人邮箱</param>
        /// <param name="mailFromName">发件人称呼</param>
        /// <param name="mailAddress">收件人邮箱</param>
        /// <param name="HostIP">SMTP地址</param>
        /// <param name="Port">SMTP端口</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public static bool sendMail(string mailSubjct, string mailBody, string mailFrom, string mailFromName, List<string> mailAddress, string HostIP, int Port, string username, string password) //发送邮件
        {
            bool sendOK;
            string error = sendMail(mailSubjct, mailBody, mailFrom, mailFromName, mailAddress, HostIP, Port, username, password, false, String.Empty, out sendOK);
            return sendOK;
        }

        public static string sendMail(string mailSubjct, string mailBody, string mailFrom, string mailFromName, List<string> mailAddress, string HostIP, int port, string username, string password, bool ssl, string replyTo, out bool sendOK) //发送邮件
        {
            sendOK = true;
            string error = "";
            try
            {
                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
                mm.IsBodyHtml = false;
                mm.Subject = mailSubjct;
                mm.Body = mailBody;
                mm.IsBodyHtml = true;
                if (!String.IsNullOrEmpty(mailFromName))
                    mm.From = new System.Net.Mail.MailAddress(mailFrom, mailFromName, Encoding.UTF8);
                else
                    mm.From = new System.Net.Mail.MailAddress(mailFrom);
                if (!String.IsNullOrEmpty(replyTo))
                {
                    System.Net.Mail.MailAddress replyToAddress = new System.Net.Mail.MailAddress(replyTo);
                    mm.ReplyTo = replyToAddress;
                }
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

                for (int i = 0; i < mailAddress.Count; i++)
                {
                    if (regex.IsMatch(mailAddress[i]))
                        mm.To.Add(mailAddress[i]);
                }
                if (mm.To.Count == 0)
                {
                    return String.Empty;
                }
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                sc.EnableSsl = ssl;
                sc.UseDefaultCredentials = false;
                System.Net.NetworkCredential nc = new System.Net.NetworkCredential(username, password);
                sc.Credentials = nc;
                sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                sc.Host = HostIP;
                sc.Port = port;
                sc.Send(mm);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                sendOK = false;
            }
            return error;
        }

        /// <summary>
        /// 返回登陆邮箱的网址
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns></returns>
        public static string GetEMailLoginUrl(string email)
        {
            if (email == String.Empty || email.IndexOf("@") <= 0)
                return String.Empty;
            int pos = email.IndexOf("@");
            email = "http://mail." + email.Substring(pos + 1);
            return email;

        }
    }
}
